package edu.lhh.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import edu.lhh.model.newstype;

public class DbUtils {
	private static final String url="jdbc:mysql://localhost:3306/no25?useUnicode=true&characterEncoding=UTF-8";
	private static final String driverClass="com.mysql.jdbc.Driver";
	private static final String username="root";
	private static final String pwd="root";
	private static Connection conn = null ;
	public static void getInstance(){
		if(conn==null){
			try {
				Class.forName(driverClass);
				conn = DriverManager.getConnection(url, username, pwd);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		}
	public static void closeDb(){
		if (conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn =null;
		}		
	}
	public static ResultSet findall(String table){
		getInstance();
		ResultSet rs = null;
		try {
			Statement st = conn.createStatement();
			rs = st.executeQuery("SELECT * FROM " + table);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rs;
		
	}
	public static boolean execute(String sql){
		getInstance();
		boolean boo = false;
		try {
			Statement st = conn.createStatement();
			boo = st.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return boo;
	}
	public static ResultSet query (String sql){
		getInstance();
		ResultSet rs  = null;
	
			
			try {
				Statement st = conn.createStatement();
				rs = st.executeQuery(sql);	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		return rs;
		
	}
	

}
